package com.erpanderp.wam.dbmigration.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;


import com.erpanderp.wam.dbmigration.model.entity.FlexEmployeeLeave;
import com.erpanderp.wam.dbmigration.model.entity.FlexLeaveBalance;
import com.erpanderp.wam.usm.model.entity.leave.EmployeeLeave;
import com.erpanderp.wam.usm.model.entity.leave.LeaveBalance;
import com.erpanderp.wam.usm.model.entity.leave.LeaveType;
import com.erpanderp.wam.util.HibernateUtil;

public class LeaveMigrationDAO {
	
	public List<FlexEmployeeLeave> getFlexEmployeeLeaves(){
		List<FlexEmployeeLeave> employeeLeavesList = null;
		System.out.println("IMPORTING RESOURCE EMPLOYEE MASTERS");
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
            String queryString = "from FlexEmployeeLeave as fel";
            Query query = session.createQuery(queryString);
            employeeLeavesList = (List<FlexEmployeeLeave>) query.list();
         
            System.out.println("FLEX EMPLOYEE MASTERS LIST SIZE IS "+employeeLeavesList.size());
            return employeeLeavesList;
            
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
	}
	
	public LeaveType getLeaveTypeByLeaveName(String leaveType){
		System.out.println("IN EMPLOYEE BY EMPLOYEEID DATA IMPORTING CH");
		LeaveType wamLeaveType = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
            String queryString = "from LeaveType l where l.leaveType=:leaveType";
            Query query = session.createQuery(queryString).setString("leaveType",leaveType);
            List temp =  (List<LeaveType>) query.list();
            if(temp != null && temp.size() > 0){
            	wamLeaveType = (LeaveType) temp.get(0);
            }
            session.getTransaction().commit();
            return wamLeaveType;
            
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}
	public List<FlexLeaveBalance> getFlexLeaveBalances(){
		System.out.println("IN FLEX LEAVE DATA IMPORT DAO");
		List<FlexLeaveBalance> flexLeaveBalanceList = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
            String queryString = "from FlexLeaveBalance as flb";
            Query query = session.createQuery(queryString);
            flexLeaveBalanceList = (List<FlexLeaveBalance>) query.list();
            System.out.println("IN FLEX LEAVE BALANCE LIST SIZE IN DAO "+flexLeaveBalanceList.size());
            session.getTransaction().commit();
            return flexLeaveBalanceList;
            
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}
	public FlexLeaveBalance getCLLeaveBalance(float CLbal){
		System.out.println("IN FLEX CL LEAVE BALANCE IMPORT");
		FlexLeaveBalance flexLeaveBalance = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
            String queryString = "from FlexLeaveBalance f where f.CLbal =:CLbal";
            Query query = session.createQuery(queryString).setFloat("CLbal", CLbal);
            List temp =  (List<FlexLeaveBalance>) query.list();
            if(temp != null && temp.size() > 0){
            	flexLeaveBalance = (FlexLeaveBalance) temp.get(0);
            }
            session.getTransaction().commit();
            return flexLeaveBalance;
            
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}
	public FlexLeaveBalance getPLLeaveBalance(float PLbal){
		System.out.println("IN FLEX PL LEAVE BALANCE IMPORT");
		FlexLeaveBalance flexLeaveBalance = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
            String queryString = "from FlexLeaveBalance f where f.PLbal =:PLbal";
            Query query = session.createQuery(queryString).setFloat("PLbal", PLbal);
            List temp =  (List<FlexLeaveBalance>) query.list();
            if(temp != null && temp.size() > 0){
            	flexLeaveBalance = (FlexLeaveBalance) temp.get(0);
            }
            session.getTransaction().commit();
            return flexLeaveBalance;
            
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}
	public FlexLeaveBalance getCurrentCLLeaveBalance(float cur_CL){
		System.out.println("IN EMPLOYEE BY EMPLOYEEID DATA IMPORTING CH");
		FlexLeaveBalance flexLeaveBalance = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
            String queryString = "from FlexLeaveBalance f where f.cur_CL =:cur_CL";
            Query query = session.createQuery(queryString).setFloat("cur_CL", cur_CL);
            List temp =  (List<FlexLeaveBalance>) query.list();
            if(temp != null && temp.size() > 0){
            	flexLeaveBalance = (FlexLeaveBalance) temp.get(0);
            }
            session.getTransaction().commit();
            return flexLeaveBalance;
            
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}
	public void createWamEmployeeLeaveBalances(List<LeaveBalance> wamLeaveBalanceList){
		System.out.println("IN WAM EMPLOYEE LEAVES DATA EXPORTING");
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for(LeaveBalance leaveBalance : wamLeaveBalanceList){
             session.save(leaveBalance);
			}
            session.getTransaction().commit();
          
            
		}catch (Exception e) {
			e.printStackTrace();
			
			// TODO: handle exception
		}
	}
	public void createWAMEmployeeLeaves(List<EmployeeLeave> wamEmployeeLeaveList){
		System.out.println("IN WAM EMPLOYEE LEAVES DATA EXPORTING");
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for(EmployeeLeave employee : wamEmployeeLeaveList){
             session.save(employee);
			}
            session.getTransaction().commit();
          
            
		}catch (Exception e) {
			e.printStackTrace();
			
			// TODO: handle exception
		}
	}

	public static void main(String a[]){
		System.out.println(new LeaveMigrationDAO().getFlexEmployeeLeaves().size());
		
	}
	
}
